package com.dotlink.resources.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.ws.rs.QueryParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dotlink.frame.filter.WebContext;
import com.dotlink.frame.utils.Const;
import com.dotlink.frame.utils.ContextHolderUtils;
import com.dotlink.frame.utils.Md5;
import com.dotlink.frame.utils.ResponseUtils;
import com.dotlink.frame.utils.StringUtil;
import com.dotlink.frame.utils.page.Pagination;
import com.dotlink.resources.entity.SysUser;
import com.dotlink.resources.service.SysUserService;

/**
 * 
 * @author <Auto generate>
 * @version 2015-05-21 23:16:41
 */
@Controller
@RequestMapping(value = "/sysuser")
public class SysUserController {
	@Autowired
	private SysUserService sysUserService;


	@RequestMapping("/list")
	public String list(SysUser sysUser) {
		
		HttpSession session = ContextHolderUtils.getSession();
		SysUser user = (SysUser) session.getAttribute("user");
		if(user.getDepartmentId() != 0){
			sysUser.setDepartmentId(user.getId());
		}
		List<SysUser> list = sysUserService.list(sysUser);
		WebContext.setAttribute("list", list);
		return "/sys/user/list";
	}

	@RequestMapping("/add")
	public String add(Long id) {
		return "/sys/user/add";
	}
	
	@RequestMapping("/reset")
	public String resetPassword(Long id) {
		return "/sys/user/reset_pwd";
	}

	@RequestMapping("/update")
	public String update(Long id) {
		WebContext.setAttribute("userId", id);
		return "/sys/user/update";
	}

	@RequestMapping("/roles")
	public String roles(Long id) {
		if (id == null) {
			id = 0L;
		}
		WebContext.setAttribute("userId", id);
		return "/sys/user/roles";
	}

	@ResponseBody
	@RequestMapping("/getById/{id}")
	public Object getById(@PathVariable Long id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", -1);
		try {
			SysUser r = sysUserService.selectSysUserById(id);
			map.put("status", 1);
			map.put("data", r);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", Const.DEFAULT_ERROR);
		}
		return map;
	}

	@ResponseBody
	@RequestMapping("/checkrole/{roleid}")
	public Object checkrole(@PathVariable Long roleid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", -1);
		try {
			List<SysUser> user = sysUserService.selectSysUserByRole(roleid);
			if (user != null && !user.isEmpty()) {
				map.put("error", "角色已分配，不能删除");
			} else {
				map.put("status", Const.SUCCESS);
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("error", Const.DEFAULT_ERROR);
		}
		return map;
	}

	@RequestMapping("/delete")
	public Object deletes(@QueryParam("id") Long id, Pagination pagination) {
		try {
			sysUserService.deleteSysUserById(id);
			return "redirect:/sysuser/list";
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseUtils.buildErrorRes(Const.GENERAL_ERROR, e.getMessage());
		}
	}

	@RequestMapping("/save")
	@ResponseBody
	public Object save(SysUser sysUser) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", -1);
		try {
			Integer i = 0;
			if (sysUser.getId() != null && sysUser.getId() != 0) {
				i = sysUserService.updateSysUserById(sysUser);
			} else {
				HttpSession session = ContextHolderUtils.getSession();
				SysUser user = (SysUser) session.getAttribute("user");
				sysUser.setDepartmentId(user.getId());
				String md5pwd = Md5.GetMD5Code(sysUser.getPassword());
				sysUser.setPassword(md5pwd);
				i = sysUserService.saveSysUser(sysUser);
			}
			if (i > 0) {
				map.put("status", 200);
				map.put("data", "保存成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", Const.DEFAULT_ERROR);
		}
		return map;
	}

	@ResponseBody
	@RequestMapping("/getRole/{userid}")
	public Object getRoleByUserId(@PathVariable Long userid) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> list = sysUserService.selectRoleByUserId(userid);
			map.put("data", list);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", Const.DEFAULT_ERROR);
		}
		return map;
	}
	
	@RequestMapping("/reset/pwd")
	@ResponseBody
	public Object resetPassword(String oldpwd, String password, String confirm) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", -1);
		try {
			if(StringUtil.isEmpty(oldpwd) || StringUtil.isEmpty(password) || StringUtil.isEmpty(confirm)){
				map.put("data", "参数不能为空");
				return map;
			}
			
			if(!password.equals(confirm)){
				map.put("data", "确认密码不正确");
				return map;
			}
			
			//判断密码是否正确
			HttpSession session = ContextHolderUtils.getSession();
			SysUser user = (SysUser) session.getAttribute("user");
			String md5pwd = Md5.GetMD5Code(oldpwd);
			if (!md5pwd.equals(user.getPassword())) {
				map.put("data", "您输入的密码不正确!");
				return map;
			}
			
			//修改密码
			user.setPassword(Md5.GetMD5Code(password));
			Integer i = sysUserService.updateSysUserByReset(user);
			if(i > 0){
				map.put("status", 200);
				session.setAttribute("user", user);
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("data", Const.DEFAULT_ERROR);
		}
		return map;
	}
	
	@RequestMapping("/getNormalUser")
	@ResponseBody
	public Object getNormalUser() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<SysUser> normalUserList = sysUserService.getNormalUser();
		map.put("normalUserList", normalUserList);
		return map;
	}
}
